#include "ProtocolMngr.h"

ProtocolMngr::ProtocolMngr(GraphicEngine_base *Engine, SoundSystem_base* Sound, SocketComm_base* pSocket, LedDriver_base* myLed, bool startup, DealProtType *pParent)
					
{
	x=0;y=0;z=0;
	AEngine = Engine;
	MainSound = Sound;
	myStimuli = myLed;
	StartUP = startup;
	MyTimer = GetTickCount();//MyTimer = AEngine->device->getTimer();
	//MyTimer->setTime(0);
	ProtXState = 0;
	ProtXCursor = 0;
	this->pSocket = pSocket;
	dealProtocol = /*dynamic_cast <DealProtocol *>*/ (pParent);
	//Di default non faccio partire ne fermo la musica quando vengono chiamate StartProt e StopProt
	DoStopMusic = false;
	DoPlayMusic = false;
	isFade=false;
	isRunning=false;
}

ProtocolMngr::~ProtocolMngr(void)
{
}

void ProtocolMngr::OnNew(void)
{
	gui::IGUIInOutFader* myfader;

	cout<<"*************************************"<<endl;
	if (StartUP==true)
		AEngine->RestartEngine();
	
	// DISPLAYING LOAD IMAGE
	AEngine->driver->beginScene(true,true,video::SColor(0,0,0,0));
	AEngine->DrawCentered2DImage(AEngine->driver->getTexture("../Media/Logos/LOADING.tga"));
	AEngine->driver->endScene();
	cout<<"...done!"<<endl;

	cout<<"*************************************"<<endl;
	cout<<"LOADING BUILT-IN RESOURCES..."<<endl;
	AEngine->LoadBuiltInResources();
	cout<<"...done!"<<endl;
	cout<<"LOADING PROTOCOL-SPECIFIC RESOURCES..."<<endl;
	LoadResources();
	cout<<"...done!"<<endl;
	cout<<"*************************************"<<endl;
	cout<<""<<endl;

	// FADING IN THE PROTOCOL
	myfader = AEngine->env->addInOutFader();
    myfader->fadeIn(3000);
}

bool ProtocolMngr::StartProt(bool SendSocketMsg)
{
	//if (DoPlayMusic) MainSound->PlayMusic();
	mySceneNode=AEngine->smgr->getSceneNodeFromName(STATUS_OFF);
	mySceneNode->setVisible(false);
	mySceneNode=AEngine->smgr->getSceneNodeFromName(STATUS_ON);
	mySceneNode->setVisible(true);

	BCIMessage	StartMsg = ComposeMessage(START_ACQ);
	if (SendSocketMsg == true)
		pSocket->SendData(&StartMsg);
	//tZero = GetTickCount();//MyTimer->getRealTime();
	isRunning = true;

	return true;
}

bool ProtocolMngr::StopProt(bool SendSocketMsg)
{
//	if (DoStopMusic) MainSound->StopMusic();
	mySceneNode=AEngine->smgr->getSceneNodeFromName(STATUS_ON);
	mySceneNode->setVisible(false);
	mySceneNode=AEngine->smgr->getSceneNodeFromName(STATUS_OFF);
	mySceneNode->setVisible(true);

	BCIMessage	StopMsg = ComposeMessage(STOP_ACQ);
	
	isRunning = false;
	myStimuli->Stop();

	ProtXCursor = 0;

	mySceneNode = AEngine->smgr->getSceneNodeFromName(CUE);
	mySceneNode->setVisible(false);
	HideCenter();
	if (SendSocketMsg == true)
	{
		Sleep(2000);
		pSocket->SendData(&StopMsg);
	}
	
	return true;
}

void ProtocolMngr::SendBCIMessage(int Kind, int Value, int *pBuffer,int BuffSize)
{
	BCIMessage Temp;
	Temp = ComposeMessage(Kind,Value,pBuffer,BuffSize);
	pSocket->SendData(&Temp);	
}

int ProtocolMngr::GetXState(void)
{
	return ProtXState;
}

void ProtocolMngr::SetNextCall(int Millis)
{
	tZero += Millis;
}

void ProtocolMngr::ResetTime(void)
{
	tZero = GetTickCount();//MyTimer->getRealTime();
}

void ProtocolMngr::ViewFeedback(void)
{
		mySceneNode = AEngine->smgr->getSceneNodeFromName(FB_LEFT);
		mySceneNode->setVisible(true); 
		mySceneNode = AEngine->smgr->getSceneNodeFromName(FB_RIGHT);
		mySceneNode->setVisible(true); 
		mySceneNode = AEngine->smgr->getSceneNodeFromName(FB_UP);
		mySceneNode->setVisible(true); 
		mySceneNode = AEngine->smgr->getSceneNodeFromName(FB_DOWN);
		mySceneNode->setVisible(true); 
}

void ProtocolMngr::HideFeedback(void)
{
		mySceneNode = AEngine->smgr->getSceneNodeFromName(FB_LEFT);
		mySceneNode->setVisible(false); 
		mySceneNode = AEngine->smgr->getSceneNodeFromName(FB_RIGHT);
		mySceneNode->setVisible(false); 
		mySceneNode = AEngine->smgr->getSceneNodeFromName(FB_UP);
		mySceneNode->setVisible(false); 
		mySceneNode = AEngine->smgr->getSceneNodeFromName(FB_DOWN);
		mySceneNode->setVisible(false); 
}

void ProtocolMngr::EvalProt(void)
{
	int VirtualTime;
	if (isRunning)
	{
  		VirtualTime = GetTickCount();//MyTimer->getRealTime();
		myProtocol(VirtualTime - tZero);
	}
}

void ProtocolMngr::RunIntoWhile(void)
{
	if (isRunning)
	{
		myRunIntoWhile();
	}
}

void ProtocolMngr::setNextOpt (int kind, int delay, int p1, char *ptext)
{
	nextOpt.kind=kind;
	nextOpt.p1=p1;
	if (ptext!=NULL)
		strcpy(&nextOpt.text[0],ptext);
	ResetTime();
	isWaiting=true;
	SetNextCall(delay);
}

void ProtocolMngr::HideCenter(void)
{
	mySceneNode=AEngine->smgr->getSceneNodeFromName(CENTER);
	mySceneNode->setVisible(false);
}

void ProtocolMngr::RotAnimator(char *NodeName, scene::ISceneNodeAnimator* anim)
{
	mySceneNode= AEngine->smgr->getSceneNodeFromName(NodeName);
	mySceneNode->addAnimator(anim);
}

void ProtocolMngr::MoveAnimator(char *NodeName, float x, float y, float z, int time)
{
	mySceneNode= AEngine->smgr->getSceneNodeFromName(NodeName);	
	AEngine->movement = AEngine->smgr->createFlyStraightAnimator((mySceneNode->getPosition()),(mySceneNode->getPosition()+core::vector3df((f32)x,(f32)y,(f32)z)),time,false);
	mySceneNode->addAnimator(AEngine->movement);	
}


bool ProtocolMngr::OnSocketEvent(BCIMessage *SocketMsg)
{
	switch (SocketMsg->Kind)
	{
		case CLASSIFICATION:
			OnClassification(SocketMsg);
			return true;
		break;
			
		case FEEDBACK:
			OnFeedback(SocketMsg);
			return true;
		break;
		
		case CONNECTION_COMPLETED:
			StartProt(false);
			return true;
		break;

		case SET_PARAMS:
			OnSetParams(SocketMsg);
			return true;
		break;

		default:
			cout<<"EventReceiver -> UNSUPPORTED MESSAGE "<<endl;
			return false;
		break;
	}		
}

void ProtocolMngr::SendStart(void)
{
	BCIMessage	StartMsg = ComposeMessage(START_ACQ);
	pSocket->SendData(&StartMsg);
}

void ProtocolMngr::RequestParams(void)
{
	BCIMessage	StartMsg = ComposeMessage(REQUEST_PARAMS);
	pSocket->SendData(&StartMsg);
}

void ProtocolMngr::ViewCenter(void)
{
	mySceneNode= AEngine->smgr->getSceneNodeFromName(CENTER);	
	mySceneNode->setVisible(true);
}

void ProtocolMngr::myProtocol(int myTime)
{}

void ProtocolMngr::myRunIntoWhile(void)
{}

void ProtocolMngr::OnProtocolEvent(void)
{}

void ProtocolMngr::LoadResources(void)
{}

void ProtocolMngr::OnClassification(BCIMessage *SocketMsg)
{}

void ProtocolMngr::OnFeedback(BCIMessage *SocketMsg)
{}

void ProtocolMngr::OnSetParams(BCIMessage *SocketMsg)
{}

void ProtocolMngr::KeyEvent(const irr::SEvent &event)
{}

bool ProtocolMngr::OnGuiEvent(const irr::SEvent &event)
{
	return true;
}
void ProtocolMngr::OtherEvent(const irr::SEvent &event)
{
}